<template>
    <!-- 顶部导航 -->
    <van-nav-bar title="编辑收货地址" left-text="返回" left-arrow @click-left="onClickLeft" />

    <van-address-edit
        :area-list="areaList"
        show-set-default
        :address-info="addressInfo"
        show-delete
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
    />
</template>
<script>
import { areaList } from '@vant/area-data';
import {showNotify} from 'vant'
export default {
    created() {
        let addrid = this.$route.query.addrid

        this.addrid = addrid

        this.info()
    },
    data() {
        return {
            areaList:areaList,
            LoginUser:this.$cookies.get('LoginAuth'),
            addrid:0,
            addressInfo:{}
        }
    },
    methods: {
        onClickLeft()
        {
            this.$router.go(-1)
        },
        async info()
        {
            let data = {
                addrid:this.addrid,
                busid:this.LoginUser.id
            }

            let result = await this.$api.AddressInfo(data)

            if(result.code === 0)
            {
                showNotify({
                    type:'warning',
                    message:result.msg
                })

                this.$router.go(-1)

                return false
            }

            let address = result.data

            this.addressInfo = {
                name:address.consignee,
                tel:address.mobile,
                addressDetail:address.address,
                areaCode:address.district,
                isDefault:address.status == 1 ? true : false,
            }
        },
        async onSave(value)
        {
            let data = {
                busid:this.LoginUser.id,
                consignee:value.name,
                mobile:value.tel,
                code:value.areaCode,
                status:value.isDefault ? 1:0,
                address:value.addressDetail,
                addrid:this.addrid
            }

            let result = await this.$api.AddressEdit(data)

            if(result.code === 1)
            {
                showNotify({
                    type:'success',
                    message:result.msg,
                    onClose:() => {
                        this.$router.push('/user/address/index')
                    }
                })

                return false
            }else{
                showNotify({
                    type:'warning',
                    message:result.msg
                })

                return false
            }
        },
        onDelete()
        {

        }
    },
}
</script>